package com.wyp168.leetcode;

/**
 * @ClassName CanJump
 * @Author wyp168
 * @Description: leetcode: 55. 跳跃游戏
 * @Date 2022/10/4 20:40
 */
public class CanJumpSolution {
    public static void main(String[] args) {
        CanJumpSolution solution = new CanJumpSolution();

        int[] nums = {3,2,1,0,4};

        System.out.println(solution.canJump(nums));
    }

    public boolean canJump(int[] nums) {

        //定义一个当前可跳的最大距离
        int maxDistance = 0;

        for (int i = 0; i < nums.length; i++) {
            if (maxDistance < i) {
                return false;
            }

            if (maxDistance < (i + nums[i])) {
                maxDistance = i + nums[i];
            }

            if (maxDistance >= nums.length)
                return true;
        }

        return true;
    }
}
